<?php
/**
 * 用户类
 * Class User
 */

class User
{

    static function IsAuthenticated()
    {
        if (!isset($_SESSION["WANZ_USERNAME"])) {
            return false;
        }
        return true;
    }

    static function GetCurrentUser()
    {
        if (User::IsAuthenticated()) {
            $username = $_SESSION["WANZ_USERNAME"];
            $ci = &get_instance();
            $dbObj = $ci->load->database('default', true);
            $dbObj->where('name', $username);
            $userInfo = $dbObj->get('admin')->row();
            $userInfo->roleArray = explode(',', $userInfo->name);
            return $userInfo;
        }
    }

    //判断用户和密码是否存在
    static function LogInUser($username, $password)
    {
        $password = sha1($password);
        $ci = &get_instance();
        $dbObj = $ci->load->database('default', true);
        $dbObj->group_start();
        $dbObj->where('name', $username);
        $dbObj->where('pass', $password);
        $dbObj->group_end();
        $result = $dbObj->select('count(*) as num')->get('admin')->row();
        return $result->num > 0 ? true : false;
    }

    static function Get_UserCount()
    {
        $ci = &get_instance();
        $dbObj = $ci->load->database('default', true);
        return $dbObj->count_all_results('admin');

    }

    static function Get_UserList($limit, $offset)
    {
        $ci = &get_instance();
        $dbObj = $ci->load->database('default', true);
        return $dbObj->get('admin', $limit, $offset)->result();

    }

}
